package com.habdqn.attendance.web.admin;

import com.alibaba.fastjson.JSON;
import com.habdqn.attendance.dto.LayUIData;
import com.habdqn.attendance.entity.Department;
import com.habdqn.attendance.entity.User;
import com.habdqn.attendance.entity.example.UserExample;
import com.habdqn.attendance.service.DepartmentService;
import com.habdqn.attendance.service.UserService;
import com.habdqn.attendance.service.impl.DepartmentServiceImpl;
import com.habdqn.attendance.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author XingJiejian
 * @date 2019/10/26 0026
 */
@WebServlet(name = "UserListServlet", urlPatterns = "/admin/user/list")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 组装查询参数
        UserExample userExample = new UserExample();
        userExample.setName(request.getParameter("name"));
        userExample.setUsername(request.getParameter("username"));
        String strGender = request.getParameter("gender");
        if (strGender != null && !"".equals(strGender)) {
            userExample.setGender(Byte.parseByte(strGender));
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String strBirthdayBegin = request.getParameter("birthdayBegin");
        if (strBirthdayBegin != null && !"".equals(strBirthdayBegin)) {
            userExample.setBirthdayBegin(LocalDate.parse(strBirthdayBegin, dtf));
        }
        String strBirthdayEnd = request.getParameter("birthdayEnd");
        if (strBirthdayEnd != null && !"".equals(strBirthdayEnd)) {
            userExample.setBirthdayEnd(LocalDate.parse(strBirthdayEnd, dtf));
        }
        userExample.setMobile(request.getParameter("mobile"));
        String strDepartmentId = request.getParameter("departmentId");
        if (strDepartmentId != null && !"".equals(strDepartmentId)) {
            userExample.setDepartmentId(Integer.parseInt(strDepartmentId));
        }
        // 关联部门表
        userExample.setJoinDepartment(true);
        // 只查询普通用户
        userExample.setIsAdmin((byte) 0);

        // 先查询总结记录数
        UserService userService = new UserServiceImpl();
        int count = userService.getAllCount(userExample);

        // 再组装分页条件，查询分页记录
        String strLimit = request.getParameter("limit");
        int limit = 10;
        if (strLimit != null && !"".equals(strLimit)) {
            limit = Integer.parseInt(strLimit);
            userExample.setLimit(limit);
        }
        String strPageNo = request.getParameter("page");
        int pageNo = 1;
        if (strPageNo != null && !"".equals(strPageNo)) {
            pageNo = Integer.parseInt(strPageNo);
            userExample.setOffset((long) ((pageNo - 1) * limit));
        }
        List<User> departments = userService.queryAll(userExample);


        //组装响应对象
        LayUIData data = new LayUIData(0, "", count, departments);

        // 响应数据
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSON.toJSONString(data));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 获取所有部门数据
        DepartmentService departmentService = new DepartmentServiceImpl();
        List<Department> depts = departmentService.queryAll();
        request.setAttribute("depts", depts);
        request.getRequestDispatcher("/WEB-INF/jsp/admin/user/list.jsp").forward(request, response);
    }
}
